import { createStore } from 'vuex'
import { getSessionStorage, setSessionStorage } from '@/utils/common.js'

export default createStore({
  state: {
    tabs: [
      // {
      //   title: "床位示意图",
      //   path: "/bed/bedMap"
      // }
    ],
    menus: getSessionStorage('menuList') || [],
  },
  getters: {
    /**
     * 获取所有的Tab
     * @param state
     * @returns 
     */
    tabs(state) {
      return state.tabs;
    }
    /**
     * 获取所有的菜单
     * @param state
     * @returns 
     */
    , menus(state) {
      return state.menus;
    }
  },
  mutations: {
    addMenu(state, param) {
      state.menus = param;
      setSessionStorage('menuList', param);
    },
    deleteTabByIndex(state, index) {
      state.tabs.splice(index, 1);
    },
    /**
     * 清空tab
     */
    classTab(state, param) {
      state.tabs = param;
    },
    /**
     * 向vuex中添加tab
     * @param state
     * @param payload
     */
    addTab(state, payload) {
      let path = payload.path;
      if (path) {
        let result = state.tabs.filter((item) => {
          return item.path === path;
        });
        if (result.length == 0) {
          if (state.tabs.length == 10) {
            state.tabs.splice(1, 1)
          }
          state.tabs.push(payload);
        }
      }
    },
  },
  actions: {},
  modules: {},
})